<?php

function hasAFLTeamBeenSubmitted($submittedTeams, $team) {
	$count = count($submittedTeams);
	for ($i = 0; $i < $count; $i++) {
		if ($team == $submittedTeams[$i]) {
			return true;
		}
	}
	
	return false;
}

function getSelectedTeams($leagueId) {
	$selectedTeams = array();
	$teams = getTeams($leagueId, false);
	$count = count($teams);
	for ($i = 0; $i < $count; $i++) {
		$selectedTeams[] = new SelectedTeam(getSelectedTeam($teams[$i]->id), $teams[$i]->teamName);
	}
	
	return $selectedTeams;
}

function getSelectedTeamsForWeek($leagueId, $justSubmitted, $round) {
	$selectedTeams = array();
	$teams = getTeams($leagueId, false);
	$count = count($teams);
        $currentRound = getCurrentRound();
	for ($i = 0; $i < $count; $i++) {
		if ((!$justSubmitted && $teams[$i]->teamSubmittedStatus != TeamSubmittedStatus::Not_Submitted) || ($justSubmitted && $teams[$i]->teamSubmittedStatus == TeamSubmittedStatus::Submitted) || $round < $currentRound) {
			if ($round == $currentRound) {
				$thisWeeksTeam = getSelectedTeamForCurrentRound($teams[$i]->id, $round);
			}
			else {
				$thisWeeksTeam = getSelectedTeamForRound($teams[$i]->id, $round);
			}
			$lastWeeksTeam = null;
			$outs = array();
			$ins = array();
			if ($round > 1) {
				$lastWeeksTeam = getSelectedTeamForRound($teams[$i]->id, $round - 1);
				usort($thisWeeksTeam, "compareTeamPlayer");
				usort($lastWeeksTeam, "compareTeamPlayer");
				$count2 = count($thisWeeksTeam);
				$lastWeekCount = 0;
				$thisWeekCount = 0;
				
				while ($thisWeekCount < $count2 || $lastWeekCount < $count2) {
					if ($lastWeekCount == $count2) {
                    	if (!isEmergency($thisWeeksTeam[$thisWeekCount]->playerPosition) && !findPlayer($lastWeeksTeam, $thisWeeksTeam[$thisWeekCount]->player->id) && $thisWeeksTeam[$thisWeekCount]->playerPosition != null) {
							$ins[] = $thisWeeksTeam[$thisWeekCount]->player;
                        }
						$thisWeekCount++;
					}
					else if ($lastWeekCount < $count2 && $thisWeekCount == $count2) {
                                           if (!isEmergency($lastWeeksTeam[$lastWeekCount]->playerPosition) && !findPlayer($thisWeeksTeam, $lastWeeksTeam[$lastWeekCount]->player->id) && $lastWeeksTeam[$lastWeekCount]->playerPosition != null) {
				           $outs[] = $lastWeeksTeam[$lastWeekCount]->player;
                                            }
						$lastWeekCount++;
					}
					else if (!findPlayer($lastWeeksTeam, $thisWeeksTeam[$thisWeekCount]->player->id) && !isEmergency($thisWeeksTeam[$thisWeekCount]->playerPosition) && $thisWeeksTeam[$thisWeekCount]->playerPosition != null) {
						$ins[] = $thisWeeksTeam[$thisWeekCount]->player;
						$thisWeekCount++;						
					}
					else if (!findPlayer($thisWeeksTeam, $lastWeeksTeam[$lastWeekCount]->player->id) 
								&& !isEmergency($lastWeeksTeam[$lastWeekCount]->playerPosition) && $lastWeeksTeam[$lastWeekCount]->playerPosition != null) {
				        $outs[] = $lastWeeksTeam[$lastWeekCount]->player;
						$lastWeekCount++;									
					}
					else if (isset($thisWeeksTeam[$thisWeekCount]) && $thisWeeksTeam[$thisWeekCount]->playerPosition == null && !findPlayer($lastWeeksTeam, $thisWeeksTeam[$thisWeekCount]->player->id)) {
						$thisWeekCount++;
					}
					else if (isset($lastWeeksTeam[$lastWeekCount]) && $lastWeeksTeam[$lastWeekCount]->playerPosition == null && !findPlayer($thisWeeksTeam, $lastWeeksTeam[$lastWeekCount]->player->id)) {
						$lastWeekCount++;
					}
					else {
						$lastWeekCount++;
						$thisWeekCount++;
					}
				}
			}
			 

			$submittedTeam = new SelectedTeam($thisWeeksTeam, $teams[$i]->teamName);
			if (count($ins) > 0) {
				$submittedTeam->ins = $ins;
				$submittedTeam->outs = $outs;
			}
			$selectedTeams[] = $submittedTeam;
		}
	}
	
	return $selectedTeams;
}

function isEmergency($position) {
   return $position == "E1" || $position == "E2" || $position == "E3" || $position == "E4" || $position == "E5";
}

function findPlayer($players, $id) {
	foreach ($players as $player) {
		if ($player->playerPosition != null && $player->player->id == $id && !isEmergency($player->playerPosition)) {
			return true;
		}
	}
	
	return false;
}

function isTeamPartiallyLockedOut($partialLockoutTeams, $team) {
	$count = count($partialLockoutTeams);
	for ($k = 0; $k < $count; $k++) {
		if ($partialLockoutTeams[$k] == $team) {
			return true;
		}
	}
	
	return false;
}

function getTeamDisplayForForum($team) {
	$result = "[table2][r][c][b]" . $team->teamName . "[/b][/c][/r]";
	$result .= "[r][c][b]B:[/b][/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('LBP')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('FB')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('RBP')->player) . "[/c]";
	$result .= "[/r]";
	$result .= "[r][c][b]HB:[/b][/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('LHBF')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('CHB')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('RHBF')->player) . "[/c]";
	$result .= "[/r]";	
	$result .= "[r][c][b]C:[/b][/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('LW')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('C')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('RW')->player) . "[/c]";
	$result .= "[/r]";	
	$result .= "[r][c][b]HF:[/b][/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('LHFF')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('CHF')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('RHFF')->player) . "[/c]";
	$result .= "[/r]";	
	$result .= "[r][c][b]F:[/b][/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('LFP')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('FF')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('RFP')->player) . "[/c]";
	$result .= "[/r]";	
	$result .= "[r][c][b]R:[/b][/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('R')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('F1')->player) . "[/c]";
	$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('F2')->player) . "[/c]";
	$result .= "[/r]";	
	$result .= "[r][c][b]EMG:[/b][/c]";
	if ($team->getPlayer('E1') != null) {
		$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('E1')->player) . "[/c]";
	}
	if ($team->getPlayer('E2') != null) {
		$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('E2')->player) . "[/c]";
	}
	if ($team->getPlayer('E3') != null) {
		$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('E3')->player) . "[/c]";
	}
	$result .= "[/r]";
	$result .= "[r][c][/c]";
	if ($team->getPlayer('E4') != null) {
		$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('E4')->player) . "[/c]";
	}
	if ($team->getPlayer('E5') != null) {
		$result .= "[c]" . getPlayerDisplayForForum($team->getPlayer('E5')->player) . "[/c]";
	}
	$result .= "[c][/c]";
	$result .= "[/r][/table2]";
	
	return $result;
}

function setDefaultTeams($coachesNotSubmittedTeam) {
	$round = getCurrentRound();
	foreach ($coachesNotSubmittedTeam as $teamId) {
		setDefaultTeam($teamId, $round);
	}
}
?>